﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Bug_Speed
{
    public partial class FrmBugList : Form
    {
        //excel data
        private DataTable _excelData;

        public FrmBugList()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        public DataTable ExcelData
        {
            set { _excelData = value; }
            get { return _excelData; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBugList_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = _excelData;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (treeView1.SelectedNode.Text != "All bugs")
                {
                    ExcelData.DefaultView.RowFilter = "F3='" + treeView1.SelectedNode.Text + "'";
                }
                else
                {
                    ExcelData.DefaultView.RowFilter = "F3<>''";
                }

                DataTable dt = ExcelData.DefaultView.ToTable();

                dataGridView1.DataSource = dt;

                TxtbugType.Text = treeView1.SelectedNode.Text.ToString();
                TxtCurrentbugCnt.Text = dt.Rows.Count.ToString();
                getOwnerInfo();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private void getOwnerInfo()
        {
            try
            {
                #region"计算每个人的bug数量"
                if (treeView1.SelectedNode.Text != "All bugs")
                {
                    ExcelData.DefaultView.RowFilter = "F3='" + treeView1.SelectedNode.Text + "'";
                }
                else
                {
                    ExcelData.DefaultView.RowFilter = "F3<>''";
                }

                DataTable orignal = ExcelData.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='jackie.lee.2009'";
                DataTable dt1 = orignal.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='alphateam.zhang'";
                DataTable dt2 = orignal.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='luckjia'";
                DataTable dt3 = orignal.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='jianghongfei'";
                DataTable dt4 = orignal.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='chenwei137'";
                DataTable dt5 = orignal.DefaultView.ToTable();

                orignal.DefaultView.RowFilter = "F5='rockychen2010'";
                DataTable dt6 = orignal.DefaultView.ToTable();

                int[] count = new int[6];

                int[] tempcount = new int[6];

                count[0] = dt1.Rows.Count;
                count[1] = dt2.Rows.Count;
                count[2] = dt3.Rows.Count;
                count[3] = dt4.Rows.Count;
                count[4] = dt5.Rows.Count;
                count[5] = dt6.Rows.Count;

                tempcount[0] = count[0];
                tempcount[1] = count[1];
                tempcount[2] = count[2];
                tempcount[3] = count[3];
                tempcount[4] = count[4];
                tempcount[5] = count[5];

                dt1 = null;
                dt2 = null;
                dt3 = null;
                dt4 = null;
                dt5 = null;
                dt6 = null;
                #endregion

                #region"取得最大和最小数(冒泡排序)"

#region "obsolete"
                int temp;
                for (int j = 0; j < 5; j++)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (count[i] > count[i + 1])
                        {
                            temp = count[i + 1];
                            count[i + 1] = count[i];
                            count[i] = temp;
                        }
                    }
                }
#endregion
                BubbleSort(count);

                txtMaxCnt.Text = count[5].ToString();
                TxtMinCnt.Text = count[0].ToString();
                #endregion

                #region"显示最小，最大bug数拥有者"

                for (int i = 0; i < 6; i++)
                {
                    if (count[0] == tempcount[i])
                    {
                        if (i == 0)
                        {
                            TxtBugMinOwner.Text = "jackie.lee.2009";
                        }
                        if (i == 1)
                        {
                            TxtBugMinOwner.Text = "alphateam.zhang";
                        }
                        if (i == 2)
                        {
                            TxtBugMinOwner.Text = "luckjia";
                        }
                        if (i == 3)
                        {
                            TxtBugMinOwner.Text = "jianghongfei";
                        }
                        if (i == 4)
                        {
                            TxtBugMinOwner.Text = "chenwei137";
                        }
                        if (i == 5)
                        {
                            TxtBugMinOwner.Text = "rockychen2010";
                        }
                    }

                }

                for (int i = 0; i < 6; i++)
                {
                    if (count[5] == tempcount[i])
                    {
                        if (i == 0)
                        {

                            TxtBugMaxOwner.Text = "jackie.lee.2009";
                        }
                        if (i == 1)
                        {
                            TxtBugMaxOwner.Text = "alphateam.zhang";
                        }
                        if (i == 2)
                        {
                            TxtBugMaxOwner.Text = "luckjia";
                        }
                        if (i == 3)
                        {
                            TxtBugMaxOwner.Text = "jianghongfei";
                        }
                        if (i == 4)
                        {
                            TxtBugMaxOwner.Text = "chenwei137";
                        }
                        if (i == 5)
                        {
                            TxtBugMaxOwner.Text = "rockychen2010";
                        }
                    }

                }

                TxtBugOwners.Text = "";

                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

          public void BubbleSort(int[] R) 　　
                { 　　　int i,j,temp; //交换标志 　　　
              bool exchange; 　　　
              for(i=0; i<R.Length; i++) //最多做R.Length-1趟排序 　　　
              { 　　　exchange=false; //本趟排序开始前，交换标志应为假 　　　
                  for(j=R.Length-2; j>=i; j--) 　//最多n-1次交换　　
                  { 　　　if(R[j+1]<R[j])　//“逆序”交换条件　　　
                  { 　　　　temp=R[j+1]; 　　　　
                      R[j+1]=R[j]; 　　　　
                      
                      R[j]=temp; 　　　　
                      exchange=true; //发生了交换，故将交换标志置为真 　　　
                  } 　　　
                  } 　　　
                  if(!exchange) //本趟排序未发生交换，提前终止算法 　　　
                  { 　　　break; 　　　
                  } 　　　
              } 　　
          }


        #region "obsolete"
        //private DataTable ConvertToDataTable(string[,] arr)
        //{

        //    DataTable dataSouce = new DataTable();
        //    //添加列名
        //    for (int i = 0; i < arr.GetLength(1); i++)
        //    {
        //        DataColumn newColumn = new DataColumn(i.ToString(), arr[0, 0].GetType());
        //        dataSouce.Columns.Add(newColumn);
        //    }
        //    //添加行
        //    for (int i = 0; i < arr.GetLength(0); i++)
        //    {
        //        DataRow newRow = dataSouce.NewRow();
        //        for (int j = 0; j < arr.GetLength(1); j++)
        //        {
        //            newRow[j.ToString()] = arr[i, j];
        //        }
        //        dataSouce.Rows.Add(newRow);
        //    }
        //    return dataSouce;

        //} 
        #endregion
    }
}
